package notify

import (
	"APT/internal/consts"
	"APT/internal/dao"
	"APT/internal/library/contexts"
	"APT/internal/library/hgorm/hook"
	"APT/internal/model/entity"
	"APT/internal/model/input/input_hotel"
	"APT/internal/service"
	"bytes"
	"context"
	"database/sql"
	"errors"
	"fmt"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/os/gtime"
	"github.com/gogf/gf/v2/util/gmeta"
	"html/template"

	"github.com/gogf/gf/v2/errors/gerror"

	"APT/api/notify/kefu"
)

func (c *ControllerKefu) OrderList(ctx context.Context, req *kefu.OrderListReq) (res *kefu.OrderListRes, err error) {
	list, totalCount, orderCount, err := service.HotelService().ReservationRoomList(ctx, &req.PmsAppReservationRoomListInp)
	if err != nil {
		return
	}

	if list == nil {
		list = []*input_hotel.PmsAppReservationRoomListModel{}
	}

	res = new(kefu.OrderListRes)
	res.List = list
	res.OrderCount = orderCount
	res.PageRes.Pack(req, totalCount)
	return
}
func (c *ControllerKefu) AppReservationLanguageTemplate(ctx context.Context, req *kefu.AppReservationLanguageTemplateReq) (res *kefu.AppReservationLanguageTemplateRes, err error) {
	var (
		appReservationInfo *AppReservationInfo
		appStay            *AppStayInfo
		Property           *entity.PmsProperty
		tmpl               *template.Template
		buffer             bytes.Buffer
	)
	g.Dump(contexts.GetLanguage(ctx))
	tmpl = new(template.Template)
	if req.Type == 1 {
		if err = dao.PmsAppReservation.Ctx(ctx).
			WherePri(req.Id).WithAll().Hook(hook.PmsFindLanguageValueHook).
			Scan(&appReservationInfo); err != nil && !errors.Is(err, sql.ErrNoRows) {
			return
		}

		if g.IsEmpty(appReservationInfo) {
			err = gerror.New("订单不存在")
			return
		}
		appReservationInfo.PmsAppReservation.CheckinTime = fmt.Sprintf("%s %s", gtime.New(appReservationInfo.CheckinDate).Format("y-m-d"), appReservationInfo.CheckinTime)
		appReservationInfo.PmsAppReservation.CheckoutTime = fmt.Sprintf("%s %s", gtime.New(appReservationInfo.CheckoutDate).Format("y-m-d"), appReservationInfo.CheckoutTime)
		switch contexts.GetLanguage(ctx) {
		case "zh":
			tmpl, err = template.New("bookingInfo").Parse(consts.TemplateReservationZh)
			break
		case "zh_CN":
			tmpl, err = template.New("bookingInfo").Parse(consts.TemplateReservationZhCN)
			break
		case "en":
			tmpl, err = template.New("bookingInfo").Parse(consts.TemplateReservationEn)
			break
		case "ja":
			tmpl, err = template.New("bookingInfo").Parse(consts.TemplateReservationJa)
			break
		case "ko":
			tmpl, err = template.New("bookingInfo").Parse(consts.TemplateReservationKo)
			break
		default:
			err = gerror.New("语言格式错误")
		}

	} else if req.Type == 2 {
		if err = dao.PmsAppStay.Ctx(ctx).
			WherePri(req.Id).WithAll().Hook(hook.PmsFindLanguageValueHook).
			Scan(&appStay); err != nil && !errors.Is(err, sql.ErrNoRows) {
			return
		}
		if g.IsEmpty(appStay) {
			err = gerror.New("订单不存在")
			return
		}
		switch contexts.GetLanguage(ctx) {
		case "zh":
			tmpl, err = template.New("bookingInfo").Parse(consts.TemplateOrderZh)
			break
		case "zh_CN":
			tmpl, err = template.New("bookingInfo").Parse(consts.TemplateOrderZhCN)
			break
		case "en":
			tmpl, err = template.New("bookingInfo").Parse(consts.TemplateOrderEn)
			break
		case "ja":
			tmpl, err = template.New("bookingInfo").Parse(consts.TemplateOrderJa)
			break
		case "ko":
			tmpl, err = template.New("bookingInfo").Parse(consts.TemplateOrderKo)
			break
		default:
			err = gerror.New("语言格式错误")
		}
	} else if req.Type == 3 {
		if err = dao.PmsProperty.Ctx(ctx).
			WherePri(req.Id).Hook(hook.PmsFindLanguageValueHook).
			Scan(&Property); err != nil && !errors.Is(err, sql.ErrNoRows) {
			return
		}
		if g.IsEmpty(Property) {
			err = gerror.New("物业不存在")
			return
		}
		switch contexts.GetLanguage(ctx) {
		case "zh":
			tmpl, err = template.New("bookingInfo").Parse(consts.TemplatePropertyZh)
			break
		case "zh_CN":
			tmpl, err = template.New("bookingInfo").Parse(consts.TemplatePropertyZhCN)
			break
		case "en":
			tmpl, err = template.New("bookingInfo").Parse(consts.TemplatePropertyEn)
			break
		case "ja":
			tmpl, err = template.New("bookingInfo").Parse(consts.TemplatePropertyJa)
			break
		case "ko":
			tmpl, err = template.New("bookingInfo").Parse(consts.TemplatePropertyKo)
			break
		default:
			err = gerror.New("语言格式错误")
		}
	}
	if err != nil {
		return
	}
	if req.Type == 1 {
		if err = tmpl.Execute(&buffer, appReservationInfo); err != nil {
			return
		}
	} else if req.Type == 2 {
		if err = tmpl.Execute(&buffer, appStay); err != nil {
			return
		}
	} else if req.Type == 3 {
		if err = tmpl.Execute(&buffer, Property); err != nil {
			return
		}
	}

	res = new(kefu.AppReservationLanguageTemplateRes)
	res.Template = buffer.String()

	return
}

type AppReservationInfo struct {
	entity.PmsAppReservation
	GuestProfileDetail *struct {
		gmeta.Meta `orm:"table:hg_pms_guest_profile"`
		*entity.PmsGuestProfile
	} `json:"guestProfileDetail" orm:"with:uid=mainGuest"`
	PropertyDetail *struct {
		gmeta.Meta `orm:"table:hg_pms_property"`
		*entity.PmsProperty
	} `json:"propertyDetail" orm:"with:uid=puid"`
	RoomTypeDetail *struct {
		gmeta.Meta `orm:"table:hg_pms_room_type"`
		*entity.PmsRoomType
	} `json:"roomTypeDetail" orm:"with:uid=room_type"  dc:"房型信息"`
}

type AppStayInfo struct {
	entity.PmsAppStay
	GuestProfileDetail *struct {
		gmeta.Meta `orm:"table:hg_pms_guest_profile"`
		*entity.PmsGuestProfile
	} `json:"guestProfileDetail" orm:"with:uid=booker"`
	PropertyDetail *struct {
		gmeta.Meta `orm:"table:hg_pms_property"`
		*entity.PmsProperty
	} `json:"propertyDetail" orm:"with:uid=puid"`
}
